﻿/*************************************************************************
* @filename : Form1.cs
* @function : 主程序代码
* @author   : Tuphie
* @version  : 1.00
* @date     : 2011-03-23
* @direction: 科院宽带系统登录客户端
**************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace netLog
{
    public partial class Form1 : Form
    {

        public string link = null;//连接状态窗口用户选择的按键
        public int shut = 0;//是否断开
        Form3 checkState;//连接成功窗体
        
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Closing += new CancelEventHandler(Form1_Closing);//重定义关闭按钮处理
            this.groupBox1.Paint += new PaintEventHandler(groupBox1_Paint);//重定义groupbox1边框显示
        }

        List<User> users;//声明一个用户的泛型集合  
        private void Form1_Load(object sender, EventArgs e)
        {
            notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;//添加系统托盘图标右键菜单

            //加载用户信息文件
            if (File.Exists("userInfo.dll"))
            {
                FileStream fs = new FileStream("userInfo.dll", FileMode.Open, FileAccess.Read);//读取用户信息文件
                BinaryFormatter bf = new BinaryFormatter();//创建一个序列化和反序列化类的对象 
                if (fs != null)
                {
                    try
                    {
                        users = (List<User>)bf.Deserialize(fs);//调用反序列化方法，从文件userInfo.dll中读取对象信息
                    }
                    catch (System.Exception error)
                    {
                        MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }

                }

                //将集合中的用户登录ID读取到下拉框中
                if (users.Count != 0)
                {
                    for (int i = 0; i < users.Count; i++)
                    {
                        if (i == 0)//第一个用户已经在框中显示了  
                        {
                            checkBox1.Checked = true;
                            textBox2.Text = users[i].LoingPassword;
                        }
                        comboBox1.Items.Add(users[i].LoginName.ToString());
                    }
                }

                fs.Close();//关闭文件流

                //学号框显示第一条记录的学号
                if (comboBox1.Items.Count != 0 && users.Count != 0)
                {
                    try
                    {
                        comboBox1.SelectedIndex = 0;
                    }
                    catch (System.Exception error)
                    {
                        MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            else
            {
                users = new List<User>();
            }

            comboBox1.Items.Add("清空历史记录...");//comboBox增加清空历史一项
        }

        /*****************************************************
         * 连接按钮触发响应
         *****************************************************/
        public CookieContainer cc = new CookieContainer();
        private void button1_Click(object sender, EventArgs e)
        {
            string id_num = comboBox1.Text.Trim();//学号
            string passw = textBox2.Text.Trim();//密码
            string mode = null;//连接模式
            int x = 0, y = 0;//鼠标单击位置坐标
            string value = null;//连接模式代号

            //判断学号是否符合要求
            if (id_num.Length != 15)
            {
                MessageBox.Show("请输入15位学号!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }       

            Regex regex = new Regex("^[A-Za-z0-9]+$");

            if (!regex.IsMatch(id_num))//含有非数字或字母的符号
            {
                MessageBox.Show("输入的学号含有非法字符,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //判断密码是否符合要求
            if (passw.Length == 0)
            {
                MessageBox.Show("请输入登录密码!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //遍历下拉框中的所有元素,如果当前登录用户在下拉列表中已经存在,则将其移除
            for (int i = 0; i < comboBox1.Items.Count; i++)  
            {
                if (comboBox1.Items[i].ToString() == id_num)
                {
                    comboBox1.Items.RemoveAt(i);    
                    break;
                }
            }

            //遍历用户集合中的所有元素,如果当前登录用户在用户集合中已经存在,则将其移除  
            for (int i = 0; i < users.Count; i++)  
            {
                if (users[i].LoginName == id_num)
                {
                    users.RemoveAt(i);
                    break;
                }
            }

            //每次都将最后一个登录的用户放插入到第一位
            try
            {
                comboBox1.Items.Insert(0, id_num);  
            }
            catch (System.Exception error)
            {
                MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            //处理用户是否愿意记住密码
            User user;

            if (checkBox1.Checked)//如果用户要求要记住密码  
            {
                string newPwd = MyEncrypt.EncryptDES(passw);//如果用户要求记住密码则对该密码进行加密
                user = new User(id_num, newPwd);//将登录ID和密码一起插入到用户集合中  
            }
            else
            {
                user = new User(id_num, "");//否则只插入一个用户名到用户集合中,密码设为空
            }

            try
            {
                users.Insert(0, user);//在用户集合中插入一个用户
            }
            catch (System.Exception error)
            {
                MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }


            //判断用户选择哪种上网模式
            if (radioButton1.Checked)
            {
                mode = "连线城域";
                value = "0";
                x = 52;
                y = 24;
            }
            else if (radioButton2.Checked)
            {
                mode = "连线国内";
                value = "1";
                x = 40;
                y = 28;
            }
            else if (radioButton3.Checked)
            {
                mode = "连线国际";
                value = "2";
                x = 65;
                y = 18;
            }

            //选择HTTP连接方式
            //利用Tamper Data截获HTTP POST请求数据包如下
            //POSTDATA=logintype="这里填连接模式"&loginuser="这里填学号"&domainid=2&password="这里填密码"
            //&refer=https%3A%2F%2Fauth.gucas.ac.cn%2Fphp%2Fonlinestatus.php%3Flogout_offnet%3D1&x=65&y=18
            if (radioButton4.Checked)
            {
                //隐藏窗体,显示连接状态窗口
                this.Visible = false;
                string url = "https://auth.gucas.ac.cn/php/user_login.php";//科院宽带系统地址
                System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();

                //发送POST请求数据包
                HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url);
                request1.Method = "POST";//请求类型
                request1.ContentType = "application/x-www-form-urlencoded";//内容类型
                request1.KeepAlive = true;//连接类型
                request1.ProtocolVersion = HttpVersion.Version10;//协议版本
                request1.Credentials = System.Net.CredentialCache.DefaultCredentials;//证书类型
                request1.Referer = "http://210.77.16.29/index.php";//Referer
                request1.CookieContainer = cc;//Cookie
                request1.Proxy = null;

                string postString = System.Web.HttpUtility.UrlEncode("logintype");//参数经过URL编码
                postString += "=" + System.Web.HttpUtility.UrlEncode(mode, Encoding.Default);//按照UTF-8编码方式进行处理,没加Encoding.Default则按本地设置编码方式进行处理,结果不一样
                postString += "&" + System.Web.HttpUtility.UrlEncode("loginuser");
                postString += "=" + System.Web.HttpUtility.UrlEncode(id_num);
                postString += "&" + System.Web.HttpUtility.UrlEncode("domainid");
                postString += "=" + System.Web.HttpUtility.UrlEncode("2");
                postString += "&" + System.Web.HttpUtility.UrlEncode("password");
                postString += "=" + System.Web.HttpUtility.UrlEncode(passw);
                postString += "&" + System.Web.HttpUtility.UrlEncode("refer");
                postString += "=" + System.Web.HttpUtility.UrlEncode("https://auth.gucas.ac.cn/php/onlinestatus.php?logout_offnet=1");
                postString += "&" + System.Web.HttpUtility.UrlEncode("x");
                postString += "=" + System.Web.HttpUtility.UrlEncode(x.ToString());
                postString += "&" + System.Web.HttpUtility.UrlEncode("y");
                postString += "=" + System.Web.HttpUtility.UrlEncode(y.ToString());

                byte[] payload = System.Text.Encoding.UTF8.GetBytes(postString);//将URL编码后的字符串转化为字节流
                request1.ContentLength = payload.Length;//设置请求内容长度
                //获得请求流
                Stream requestStream = request1.GetRequestStream();
                requestStream.Write(payload, 0, payload.Length);//将请求参数写入流
                requestStream.Close();//关闭请求流

                //获取POST响应数据包
                HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();//获得响应流
                Stream ans_1 = response1.GetResponseStream();//读取返回页面内容
                StreamReader sr_1 = new StreamReader(ans_1, System.Text.Encoding.GetEncoding("gb2312"));
                string temp_1 = sr_1.ReadToEnd();
                response1.Close();
                if (temp_1.Contains("登录错误"))
                {
                    this.Visible = true;
                    if (comboBox1.Items.Count != 0 && users.Count != 0)
                    {
                        try
                        {
                            comboBox1.SelectedIndex = 0;
                        }
                        catch (System.Exception error)
                        {
                            MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    MessageBox.Show("学号或密码输入错误, 请重新输入!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                else
                {
                    if (response1.StatusCode == HttpStatusCode.OK)
                    {
                        //确认登录成功,没有这步刷新网页未能获取成功连接页面
                        url = "https://auth.gucas.ac.cn/php/login_net.php?mode=" + value + "&quick=yes&refer=https://auth.gucas.ac.cn/php/onlinestatus.php?logout_offnet=1";
                        HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(url);
                        request2.Method = "GET";//请求类型
                        request2.Referer = "https://auth.gucas.ac.cn/php/user_login.php";//Referer
                        request2.KeepAlive = true;//连接类型
                        request2.ProtocolVersion = HttpVersion.Version10;//协议版本
                        request2.Credentials = System.Net.CredentialCache.DefaultCredentials;//证书类型
                        request2.CookieContainer = cc;//Cookie
                        request2.Proxy = null;
                        HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
                        Stream ans_2 = response2.GetResponseStream();//读取返回页面内容
                        StreamReader sr_2 = new StreamReader(ans_2, System.Text.Encoding.GetEncoding("gb2312"));
                        string temp_2 = sr_2.ReadToEnd();
                        response2.Close();
                        if (temp_2.Contains("连线失败"))
                        {
                            this.Visible = true;
                            if (comboBox1.Items.Count != 0 && users.Count != 0)
                            {
                                try
                                {
                                    comboBox1.SelectedIndex = 0;
                                }
                                catch (System.Exception error)
                                {
                                    MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                            MessageBox.Show("用户已经在此IP连线了!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                        else
                        {
                            //获取用户剩余流量信息
                            shut = 1;
                            string newurl = "https://auth.gucas.ac.cn/php/onlinestatus.php?logout_offnet=1";
                            HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(newurl);
                            request3.Method = "GET";//请求类型
                            request3.Referer = url;//Referer
                            request3.KeepAlive = true;//连接类型
                            request3.ProtocolVersion = HttpVersion.Version10;//协议版本
                            request3.Credentials = System.Net.CredentialCache.DefaultCredentials;//证书类型
                            request3.CookieContainer = cc;//Cookie
                            request3.Proxy = null;
                            HttpWebResponse response3 = (HttpWebResponse)request3.GetResponse();
                            Stream ans_3 = response3.GetResponseStream();//读取返回页面内容
                            StreamReader sr_3 = new StreamReader(ans_3, System.Text.Encoding.GetEncoding("gb2312"));
                            string temp_3 = sr_3.ReadToEnd();
                            response3.Close();

                            //读取并计算剩余流量值
                            int n1, n2;
                            string s1 = ">";
                            string s2 = "&nbsp";
                            string s3 = "M";//国内流量单位
                            string s4 = "M";//国际流量单位
                            string warning = null;
                            n1 = temp_3.IndexOf(s1, 0) + s1.Length;//开始位置
                            n2 = temp_3.IndexOf(s2, n1);//结束位置

                            n1 = temp_3.IndexOf(s1, n2 + s2.Length) + s1.Length;
                            n2 = temp_3.IndexOf(s2, n1);

                            n1 = temp_3.IndexOf(s1, n2 + s2.Length) + s1.Length;
                            n2 = temp_3.IndexOf(s2, n1);

                            n1 = temp_3.IndexOf(s1, n2 + s2.Length) + s1.Length;
                            n2 = temp_3.IndexOf(s2, n1);
                            string traffic_1 = temp_3.Substring(n1, n2 - n1);//国内已用流量
                            float tr_1 = (float)2048 - float.Parse(traffic_1);//国内剩余流量
                            if (tr_1 < (float)512)
                            {
                                warning += "国内";
                            }
                            if (tr_1 >= (float)1024)//超过1G以G为单位
                            {
                                tr_1 = tr_1 / (float)1024;
                                s3 = "G";
                            }

                            n1 = temp_3.IndexOf(s1, n2 + s2.Length) + s1.Length;
                            n2 = temp_3.IndexOf(s2, n1);

                            n1 = temp_3.IndexOf(s1, n2 + s2.Length) + s1.Length;
                            n2 = temp_3.IndexOf(s2, n1);
                            string traffic_2 = temp_3.Substring(n1, n2 - n1);//国际已用流量
                            float tr_2 = (float)1024 - float.Parse(traffic_2);//国际剩余流量
                            if (tr_2 < (float)256)
                            {
                                warning += "国际";
                            }
                            if (tr_2 >= (float)1024)//超过1G以G为单位
                            {
                                tr_2 = tr_2 / (float)1024;
                                s4 = "G";
                            }

                            if (warning != null)
                            {
                                warning += "流量剩余不多, 请注意使用!\n";
                                warning = "本月" + warning;
                            }

                            string note = "连接成功!\n" + warning + "\n本月剩余流量(研一):\n" + "国际流量: " + tr_2.ToString("0.00") + s4 + "\n国内流量: " + tr_1.ToString("0.00") + s3;
                            this.notifyIcon1.ShowBalloonTip(5000, "提示", note, ToolTipIcon.Info);//气泡提示信息
                        }

                    }
                    else
                    {
                        this.Visible = true;
                        if (comboBox1.Items.Count != 0 && users.Count != 0)
                        {
                            try
                            {
                                comboBox1.SelectedIndex = 0;
                            }
                            catch (System.Exception error)
                            {
                                MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        MessageBox.Show("无法连接服务器!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
   
            }
            else if (radioButton5.Checked)
            {
                MessageBox.Show("未开发模块", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

        }

        /*****************************************************
         * 断开连接
         *****************************************************/
        public int logout()
        {
            shut = 0;
            string url = "https://auth.gucas.ac.cn/php/logout_offnet.php";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";//请求类型
            request.Referer = "https://auth.gucas.ac.cn/php/onlinestatus.php?logout_offnet=1";//Referer
            request.KeepAlive = true;//连接类型
            request.ProtocolVersion = HttpVersion.Version10;//协议版本
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;//证书类型
            request.CookieContainer = cc;//Cookie
            request.Proxy = null;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream ans = response.GetResponseStream();//读取返回页面内容
            StreamReader sr = new StreamReader(ans, System.Text.Encoding.GetEncoding("gb2312"));
            string temp = sr.ReadToEnd();
            response.Close();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                if (temp.Contains("登录错误"))
                {
                    this.Visible = true;
                    if (comboBox1.Items.Count != 0 && users.Count != 0)
                    {
                        try
                        {
                            comboBox1.SelectedIndex = 0;
                        }
                        catch (System.Exception error)
                        {
                            MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    MessageBox.Show("您没有登录或已经超时!", "离线失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return 0;
                }
                else
                {
                    this.Visible = true;
                    if (comboBox1.Items.Count != 0 && users.Count != 0)
                    {
                        try
                        {
                            comboBox1.SelectedIndex = 0;
                        }
                        catch (System.Exception error)
                        {
                            MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    response.Close();
                    return 1;
                }
            }
            else
            {
                MessageBox.Show("无法连接服务器!", "离线失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                response.Close();
                return 0;
            }
        }

        /*****************************************************
         * 窗口关闭处理
         *****************************************************/
        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Hide();//隐藏Form窗体
            this.notifyIcon1.ShowBalloonTip(3000, "提示", "程序运行在系统托盘,双击显示主界面!", ToolTipIcon.Info);//气泡提示信息
            e.Cancel = true;//取消关闭Form窗口的操作
        }

        /*****************************************************
         * 任务栏通知区域图标双击响应
         *****************************************************/
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            switch (shut)
            {
                //未连接状态
                case 0:
                    if (this.Visible)
                    {
                        this.Hide();
                    }
                    else
                    {
                        this.Show();
                        if (comboBox1.Items.Count != 0 && users.Count != 0)
                        {
                            try
                            {
                                comboBox1.SelectedIndex = 0;
                            }
                            catch (System.Exception error)
                            {
                                MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                    break;
                //连接成功
                case 1:
                    checkState = new Form3(this);
                    checkState.Owner = this;
                    shut = 2;
                    checkState.ShowDialog();
                    break;
                //连线状态
                case 2:
                    if (checkState.Visible)
                    {
                        checkState.Hide();
                    }
                    else
                    {
                        checkState.ShowDialog();
                    }
                    break;
            }           
        }

        /*****************************************************
         * 退出客户端
         *****************************************************/
        private void button3_Click(object sender, EventArgs e)
        {
            this.TopMost = false;
            if (MessageBox.Show("确定退出科院宽带系统客户端(Y/N)?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                if (shut != 0)
                {
                    this.logout();
                }
                try
                {
                    if (users.Count != 0)
                    {
                        for (int i = 0; i < users.Count; i++)
                        {
                            if (users[i].LoingPassword == "")//删除选择不记住密码
                            {
                                users.RemoveAt(i);
                            }
                        }
                    }
                    FileStream fs = new FileStream("userInfo.dll", FileMode.Create, FileAccess.Write);//创建一个文件流对象  
                    BinaryFormatter bf = new BinaryFormatter();//创建一个序列化和反序列化对象  
                    bf.Serialize(fs, users);//要先将User类先设为可以序列化,将用户集合信息写入到硬盘中  
                    fs.Close();//关闭文件流
                    if (checkState != null)
                    {
                        checkState.Close();
                    }

                    this.Close();//关闭窗体
                    Application.Exit();//关闭程序
                }
                catch (System.Exception error)
                {
                    MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        /*****************************************************
         * 不显示groupbox边框线
         *****************************************************/
        private void groupBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);
        }

        /*****************************************************
         * 帮助按钮响应模块
         *****************************************************/
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(Application.StartupPath.ToString() + "\\Help.chm");
            }
            catch (System.Exception error)
            {
                MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        /*****************************************************
         * 系统托盘图标右键响应
         *****************************************************/
        private void 显示ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            switch (shut)
            {
                //未连接状态
                case 0:
                    if (this.Visible)
                    {
                        this.Hide();
                    }
                    else
                    {
                        this.Show();
                        if (comboBox1.Items.Count != 0 && users.Count != 0)
                        {
                            try
                            {
                                comboBox1.SelectedIndex = 0;
                            }
                            catch (System.Exception error)
                            {
                                MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                    break;
                //连接成功
                case 1:
                    checkState = new Form3(this);
                    checkState.Owner = this;
                    shut = 2;
                    checkState.ShowDialog();
                    break;
                //连线状态
                case 2:
                    if (checkState.Visible)
                    {
                        checkState.Hide();
                    }
                    else
                    {
                        checkState.ShowDialog();
                    }
                    break;
            } 
        }

        private void 关于ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.Show();
            
        }

        private void 帮助ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(Application.StartupPath.ToString() + "\\Help.chm");
            }
            catch (System.Exception error)
            {
                MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.TopMost = false;
            if (MessageBox.Show("确定退出科院宽带系统客户端(Y/N)?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                if (shut != 0)
                {
                    this.logout();
                }
                try
                {
                    if (users.Count != 0)
                    {
                        for (int i = 0; i < users.Count; i++)
                        {
                            if (users[i].LoingPassword == "")//删除选择不记住密码
                            {
                                users.RemoveAt(i);
                            }
                        }
                    }
                    FileStream fs = new FileStream("userInfo.dll", FileMode.Create, FileAccess.Write);//创建一个文件流对象  
                    BinaryFormatter bf = new BinaryFormatter();//创建一个序列化和反序列化对象  
                    bf.Serialize(fs, users);//要先将User类先设为可以序列化,将用户集合信息写入到硬盘中  
                    fs.Close();//关闭文件流
                    if (checkState != null)
                    {
                        checkState.Close();
                    }
                    this.Close();//关闭窗体
                    Application.Exit();//关闭程序
                }
                catch (System.Exception error)
                {
                    MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }


        /*****************************************************
         * 用户选择学号下拉菜单选项
         *****************************************************/
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "清空历史记录...")//清除历史记录
            {
                if (comboBox1.Items.Count != 0 && users.Count != 0)
                {
                    try
                    {
                        comboBox1.Items.Clear();//清空学号
                        textBox2.Clear();//清空密码
                        comboBox1.Items.Add("清空历史记录...");//comboBox增加清空历史一项

                        if (File.Exists("userInfo.dll"))//清空用户信息文件
                        {
                            FileStream fs = File.Open("userInfo.dll", FileMode.Truncate, FileAccess.ReadWrite, FileShare.Read);//清空用户信息文件内容  
                            fs.Close();//关闭文件流
                            users.RemoveRange(0, users.Count);
                        }
                    }
                    catch (System.Exception error)
                    {
                        MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            else
            {
                if (users[comboBox1.SelectedIndex].LoingPassword != "")//如果用户的密码不是为空时  
                {
                    //把用户ID所对应的密码赋给密码框(这时的数据还在用户集合中)  
                    textBox2.Text = users[comboBox1.SelectedIndex].LoingPassword.ToString();
                    checkBox1.Checked = true;
                }
                else
                {
                    textBox2.Text = "";//如果用户的密码本身就是空，那只能给空值给密码框了。  
                    checkBox1.Checked = false;
                }
            }
        }

        /*****************************************************
         * 设置程序,包括修改密码等
         *****************************************************/
        private void button2_Click(object sender, EventArgs e)
        {
            int r = this.logout();
            if (r == 1)
            {
                MessageBox.Show("离线成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

    }


    public class MyPolicy : ICertificatePolicy
    {
        public bool CheckValidationResult(ServicePoint srvPoint,
            X509Certificate certificate, 
            WebRequest request,
            int certificateProblem)
        {
            //Return True to force the certificate to be accepted.
            return true;
        }
    }
}
